// common/pet_area_picker/pet_area_picker.js
const appConfig = require('../../config/config.js');
const http = require('../../utils/http.js')
var index = [0, 0, 0]

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show:{    //控制area_select显示隐藏
      type:Boolean,
      value:false
    },
    maskShow:{    //是否显示蒙层
      type: Boolean,
      value: true
    }
    
  },

  /**
   * 组件的初始数据
   */
  data: {
    provinces: {},
    citys: {},
    areas: {},
    value: [0, 0, 0],
    province: {},
    city: {},
    area: {}
  },
  //组件生命周期函数，在组件布局完成后执行
  ready:function(){
    wx.showLoading({ title: '玩命加载中...', });
    var that = this;
    //发起网络请求
    http.post(appConfig.baseUrl + 'Notice/GetRegionByParentId', { ParentId: 0 }, function (res) {
      //console.log(res.data);
      that.setData({
        provinces: res.data,
        province: res.data[0]
      })
      //发起网络请求
      http.post(appConfig.baseUrl + 'Notice/GetRegionByParentId', { ParentId: that.data.provinces[0].RegionId }, function (res) {
        //console.log(res.data);
        that.setData({
          citys: res.data,
          city: res.data[0]
        })
        //发起网络请求
        http.post(appConfig.baseUrl + 'Notice/GetRegionByParentId', { ParentId: that.data.citys[0].RegionId },      function (res) {
          //console.log(res.data);
          wx.hideLoading();//关闭加载动画
          that.setData({
            areas: res.data,
            area: res.data[0],
          })
        });
      });
    });
    
    
  },
  /**
   * 组件的方法列表
   */
  methods: {
    handlepetAreaChange:function(e){
      var that = this;
      console.log("e:" + JSON.stringify(e));
      var value = e.detail.value;
      
      
      /**
       * 滚动的是省
       * 省改变 市、区都不变
       */
      if(index[0] != value[0]){
        index = [value[0],0,0]
        //发起网络请求
        http.post(appConfig.baseUrl + 'Notice/GetRegionByParentId',
          { ParentId: that.data.provinces[value[0]].RegionId }, function (res) {
          console.log(res.data);
          if(res && res.data && res.data.length>0){
            that.setData({
              citys: res.data,
              value: [index[0], 0, 0],
              province: that.data.provinces[index[0]],
              city: res.data[0]
            });
            http.post(appConfig.baseUrl + 'Notice/GetRegionByParentId',
              { ParentId: that.data.citys[0].RegionId }, function (res) {
                console.log(res.data);
                if (res && res.data && res.data.length > 0) {
                  that.setData({
                    areas: res.data,
                    area: res.data[0]
                  });

                }
            });
          }
        });

      } else if (index[1] != value[1]){
        /**
         * 市改变了 省不变 区变
         */
        index = [value[0], value[1], 0]
        //发起网络请求
        http.post(appConfig.baseUrl + 'Notice/GetRegionByParentId', { ParentId: that.data.citys[value[1]].RegionId }, function (res) {
          //console.log(res.data);
          that.setData({
            value: [index[0], index[1], 0],
            province: that.data.provinces[index[0]],
            citys: that.data.citys,
            city: that.data.citys[index[1]],
            areas: res.data,
            area: res.data[0]
          });
          
        });

        
      } else if (index[2] != value[2]){
        /**
         * 区改变了
         */
        index = [value[0], value[1], value[2]]
        
        that.setData({
          citys: that.data.citys,
          areas: that.data.areas,
          value: [index[0], index[1], index[2]],
          province: that.data.provinces[index[0]],
          city: that.data.citys[index[1]],
          area: that.data.areas[index[2]]
        })
      }
    },
    /**
     * 确定按钮的点击事件
     */
    handlepetAreaSelect:function(e){
      //console.log("e:" + JSON.stringify(e));
      var myEventDetail = e; // detail对象，提供给事件监听函数
      var myEventOption = {}; // 触发事件的选项
      this.triggerEvent('sureSelectArea', myEventDetail, myEventOption)
    },
    /**
     * 取消按钮的点击事件
     */
    handlepetAreaCancle:function(e){
      var that = this;
      console.log("e:" + JSON.stringify(e))
      that.setData({
        show:false
      });
      var myEventDetail = e; // detail对象，提供给事件监听函数
      var myEventOption = {}; // 触发事件的选项
      this.triggerEvent('canncelSelectArea', myEventDetail, myEventOption)
    }
  }
})
